<?php

namespace app\index\controller;

use app\common\model\AppraiseModel;
use think\Controller;
use think\Db;

class Food extends Controller
{
    /**
     * 显示资源列表
     */
    public function foodList()
    {
        $kinds = input('param.kinds');

        $where['is_del'] = 0;
        $where['is_pass'] = 1;
        switch ($kinds) {
            case 1:
                $where['food_store_tag'] = 1;
                break;
            case 2:
                $where['food_store_tag'] = 2;
                break;
            case 3:
                $where['food_store_tag'] = 3;
                break;
            case 4:
                $where['food_store_tag'] = 4;
                break;
        }

        $title = input('param.title');
        if($title){
            $where['food_store_name'] = ['like','%'.$title.'%'];
        }


        //查询商家列表
        $foodList = Db::name('food_store')
                    ->field('food_store_id,food_store_name,food_store_cover,food_people_money,food_tag')
                    ->where($where)
                    ->order('sort desc')
                    ->paginate(12,false,['query' => request()->param()]);

        $list = $foodList->items();

        foreach($list as $k => $v){

            $list[$k]['food_tag'] = explode(',',$v['food_tag']);

        }

        //查询推荐商家
        unset($where['food_store_tag']);
        unset($where['food_store_name']);
        $where['is_tuijian'] = 1;
        $hotFood = Db::name('food_store')
                ->field('food_store_id,food_store_name')
                ->where($where)
                ->order('sort desc')
                ->select();



        $this->assign('foodList',$foodList);
        $this->assign('list',$list);
        $this->assign('hotFood',$hotFood);

        return view();
    }

    /**美食详情**/
    public function food($id){

        $foodStore = Db::name('food_store')->where('food_store_id',$id)->find();

        $foodStore['food_tag'] = explode(',',$foodStore['food_tag']);

        //查询前三张图片
        $foodImg = Db::name('food_store_img')->field('img_path')->where('store_id',$id)->limit(0,3)->select();

        //查询美食
        $food = Db::name('food')->field('food_title,food_cover')->where('store_id',$id)->select();

        //判断是否数收藏该酒店
        $is_collect = 0;

        if (session('uid')) {
            $find_collect = Db::name('collect')->where('type',4)->where('other_id',$id)->find();

            if (!empty($find_collect)) {
                $is_collect = 1;
            }
        }
        
        $this->assign('is_select',$is_collect);

        $this->assign('store',$foodStore);
        $this->assign('foodImg',$foodImg);
        $this->assign('food',$food);
        return view();

    }

}
